﻿
using System;
using Framework;
using Proj;
using UnityEditor;
using UnityEngine;

namespace FrameworkEditor
{
    public class BuildSettingsWindow : EditorWindow
    {
        const int WindowWidth = 420;
        const int WindowHeight = 550;
        
        [MenuItem("tools/build window")]
        public static void ShowWindow()
        {
            BuildSettingsWindow window = EditorWindow.GetWindowWithRect<BuildSettingsWindow>(new Rect(0, 0, WindowWidth, WindowHeight));
            window.titleContent = new GUIContent("Build Settings");
            window.Show();
        }
        
        bool rebuildAllAssets;
        public string[] platformOptions ;
        int platformId;
        private bool developBuild;
        private bool uprProfiler;
        private bool autoConnectProfiler;
        private bool deepProfiling;
        private bool scriptDebugging;
        private bool scriptsOnlyBuild;
        
        int buildOperationsIndex = 0;
        public string[] buildOperations = new string[] { "Only Export Project", "Only Build", "Export Project & Build", "Build All Assets & Export Project & Build" };
        public int[] buildOperationValues = new int[]
        {
            GameBuildOperation.GetValue(export:true),
            GameBuildOperation.GetValue(copyAssets:true, buildProject:true),
            GameBuildOperation.GetValue(export:true, copyAssets:true, buildProject:true),
            GameBuildOperation.BuildAll,
        };

        private int scriptBackendIndex;
        string[] scriptBackendOptions = new string[] { "Mono", "IL2CPP" };

        private void OnEnable()
        {
            platformOptions = new string[] { FrameworkSettings.Android, FrameworkSettings.IOS, FrameworkSettings.Windows, FrameworkSettings.MacOSX };
            platformId = FrameworkSettings.AndroidPlayer;
        }

        const int VerticalInterval = 10;
        private void OnGUI()
        {
            GUILayout.BeginVertical();
            GUILayout.Space(VerticalInterval);
            rebuildAllAssets = EditorGUILayout.Toggle("Rebuild All Assets", rebuildAllAssets);
            
            GUILayout.Space(VerticalInterval);
            GUILayout.Label("Platform:");
            platformId = EditorGUILayout.Popup(platformId, platformOptions);
            GUILayout.EndVertical();

            GUILayout.Space(VerticalInterval);
            GUILayout.Label("Scripting Backend (iOS只能选il2cpp) :");
            scriptBackendIndex = EditorGUILayout.Popup(scriptBackendIndex, scriptBackendOptions);
            
            GUILayout.Space(VerticalInterval);
            GUILayout.Label("Build Options:");
            developBuild = EditorGUILayout.Toggle("    Development Build", developBuild);
            
            EditorGUI.BeginDisabledGroup(developBuild == false);
            uprProfiler = EditorGUILayout.Toggle("    UPR Profiler", uprProfiler);
            if (uprProfiler)
            {
                autoConnectProfiler = false;
                deepProfiling = false;
                scriptDebugging = false;
                scriptsOnlyBuild = false;
            }

            EditorGUI.BeginDisabledGroup(uprProfiler);
            autoConnectProfiler = EditorGUILayout.Toggle("    Autoconnect Profiler", autoConnectProfiler);
            deepProfiling = EditorGUILayout.Toggle("    Deep Profiling", deepProfiling);
            scriptDebugging = EditorGUILayout.Toggle("    Script Debugging", scriptDebugging);
            scriptsOnlyBuild = EditorGUILayout.Toggle("    Scripts Only Build", scriptsOnlyBuild);
            EditorGUI.EndDisabledGroup();

            GUILayout.Space(VerticalInterval);
            GUILayout.Label("Build Operations:");
            buildOperationsIndex = EditorGUILayout.Popup(buildOperationsIndex, buildOperations);
            
            EditorGUI.EndDisabledGroup();
            GUILayout.Space(VerticalInterval);
            if (GUILayout.Button("Build"))
            {
                BuildAssets();
            }
        }

        void BuildAssets()
        {
            if (EditorApplication.isCompiling)
            {
                EditorUtility.DisplayDialog("Build", "代码在编译中，请稍候！", "ok");
                return;
            }
            
            switch (platformId)
            {
                case FrameworkSettings.AndroidPlayer:
                    AndroidBuildTool.Build(rebuildAllAssets);
                    OpenSelectFileWindow.OpenExplorer(FrameworkSettings.GetAbRootDir(platformId));
                    break;
                case FrameworkSettings.IOSPlayer:
                    break;
                case FrameworkSettings.WindowsPlayer:
                    break;
                case FrameworkSettings.OSXPlayer:
                    break;
                default:
                    break;
            }
        }
        
        private BuildOptions GetBuildOptions()
        {
            BuildOptions options = BuildOptions.None;
            if (autoConnectProfiler)
            {
                options |= BuildOptions.ConnectWithProfiler;
            }

            if (deepProfiling)
            {
                options |= BuildOptions.EnableDeepProfilingSupport;
            }

            if (scriptDebugging)
            {
                options |= BuildOptions.AllowDebugging;
            }

            if (scriptsOnlyBuild)
            {
                options |= BuildOptions.BuildScriptsOnly;
            }
            return options;
        }
    }
}